/*
 * mox_logger.c
 *
 *  Created on: Jul 12, 2010
 *      Author: jmonk
 */

#include "mox.h"
#include "mox_log.h"
#include "mox_logger.h"
#include <print.h>

void mox_logger(chanend chan_mox)
{
	mox_msg_t temp;
	unsigned i,j;
	chanend chans[8];
	chanend n_chans;
	char buffer[200];

	chans[0] = chan_mox;
	n_chans = 1;
	while (1) {
		for (j = 0; j < n_chans; j++) {
			temp = mox_chan_in_noblock(chans[j]);
			if (temp != -1) break;
		}
		if (temp == LOG_REQ_ADD) {
			if (n_chans < 8) {
				mox_chan_out_wd(chans[j], LOG_ACK);
				chans[n_chans++] = mox_chan_in_wd(chans[j]);
			} else {
				mox_chan_out_wd(chans[j], LOG_NACK);
			}
		} else if (temp == LOG_MESSAGE) {
			temp = mox_chan_in_wd(chans[j]);
			for (i = 0; i < (temp/4); i++) {
				((int *)buffer)[i] = mox_chan_in_wd(chans[j]);
			}
			temp = mox_chan_in_wd(chans[j]);
			if (temp == LOG_END) {
				printstr(buffer);
			} else {
				printstrln("There was an Error Receiving a Log");
			}
		}
	}

	return;
}
